package com.zt.chatgpt.config.ChatGLMSDK;

import com.zt.chatglm.session.OpenAiSession;
import com.zt.chatglm.session.defaults.DefaultOpenAiSessionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: zt
 * @Description: OpenAiSession 工厂配置开启
 * @create: 2024-05-22 23:08
 */
@Configuration
@EnableConfigurationProperties(ChatGLMSDKConfigProperties.class)
public class ChatGLMSDKConfig {

    @Bean(name = "chatGLMOpenAiSession")
    // 只有配置文件中开启glm，才会注入
    @ConditionalOnProperty(value = "chatglm.sdk.config.enable", havingValue = "true", matchIfMissing = false)
    public OpenAiSession openAiSession(ChatGLMSDKConfigProperties properties) {
        // 1. 配置文件读取
        com.zt.chatglm.session.Configuration configuration = new com.zt.chatglm.session.Configuration();
        configuration.setApiHost(properties.getApiHost());
        configuration.setApiSecretKey(properties.getApiSecretKey());

        // 2. 开启会话工厂
        DefaultOpenAiSessionFactory openAiSessionFactory = new DefaultOpenAiSessionFactory(configuration);

        return openAiSessionFactory.openSession();
    }
}
